/**
 * Created by guojanyu on 2016/3/1.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/news/view', {
        templateUrl: 'news/view',
        controller: 'newsListController'
    })
});
myApp.service('newsListService', function ($http) {
    var doRequest = function () {
        return $http.get('news/list');
    }
    return {
        newsList: function () {
            return doRequest();
        }
    };
});
myApp.controller('newsListController', function ($scope, $location, $http,newsListService,Notify) {
    /*获取表单数据*/
    newsListService.newsList().success(function (response) {
        $scope.news = response;
    });
    /*删除上新*/
    $scope.deleteNews = function (id,name) {
        if (confirm('确定要删除"' + name + '"吗?')) {
            $http.get('news/delete/' + id).success(function () {
                Notify.success('删除成功');
                /*重新获取页面数据目的刷新refresh*/
                newsListService.newsList().success(function (response) {
                    $scope.news = response;
                });
            }).error(function () {
                Notify.error('删除失败');
            });
        }
    };
    /*增加上新*/
    $scope.addNews=function(){
        $scope.new={};
    };
    /*更新上新*/
    $scope.updateNews=function(data){
        $scope.new =angular.copy(data);
    };
    $scope.saveNews = function () {
        $http.post('news/edit', $scope.new).success(function () {
            Notify.success("成功");
            /*重新获取页面数据目的刷新refresh*/
            newsListService.newsList().success(function (response) {
                $scope.news = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
});

